using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using JlPay.Sdk.Core;
using JlPay.Sdk.Services.Trans.C2b;
using JlPay.Sdk.Models.Trans.C2b;
using JlPay.Sdk.Models.Trans.Dto;

namespace JlPay.Sdk.Examples.Trans.C2b
{
    /// <summary>
    /// 交易 - C2B二维码支付功能使用示例
    /// </summary>
    public class QrcodePayDemo
    {
        /// <summary>
        /// 构建商品数据
        /// </summary>
        /// <returns>商品数据对象</returns>
        private static GoodsDataDto BuildGoodsData()
        {
            var goodsData = new GoodsDataDto();
            goodsData.CostPrice = "100";
            goodsData.ReceiptId = "receipt_001";
            goodsData.GoodsDetail = new List<GoodsDetailDto>
            {
                new GoodsDetailDto
                {
                    GoodsId = "001",
                    GoodsName = "测试商品1",
                    Quantity = "1",
                    Price = "50"
                },
                new GoodsDetailDto
                {
                    GoodsId = "002",
                    GoodsName = "测试商品2", 
                    Quantity = "1",
                    Price = "50"
                }
            };
            return goodsData;
        }

        /// <summary>
        /// 构建终端信息
        /// </summary>
        /// <returns>终端信息对象</returns>
        private static TerminalInfoDto BuildTerminalInfo()
        {
            var terminalInfo = new TerminalInfoDto();
            terminalInfo.NetworkLicense = "123456789";
            terminalInfo.DeviceType = "01";
            terminalInfo.SerialNum = "TERM001";
            return terminalInfo;
        }

        /// <summary>
        /// 构建二维码支付请求
        /// </summary>
        /// <returns>请求对象</returns>
        private static QrcodePayRequest BuildQrcodePayRequest()
        {
            var request = new QrcodePayRequest();
            
            // 设置请求参数
            request.MchId = "849584358120018";
            request.TermNo = "60131234";
            request.PayType = "wxpay";
            request.OutTradeNo = "ld201911023465529";
            request.PaymentValidTime = "10";
            request.Body = "充值中心-会员充值";
            request.Attach = "商城—-易拉宝10个打印纸20箱";
            request.TotalFee = "1";
            request.LimitPay = "no_credit";
            request.IsHirePurchase = "1";
            request.HirePurchaseNum = "3";
            request.HirePurchaseSellerPercent = "0";
            request.Remark = "该订单物流请走顺丰快递";
            request.OpUserId = "001";
            request.OpShopId = "GD_SZ_001";
            request.DeviceInfo = "N50007A8890";
            request.MchCreateIp = "123.12.12.123";
            request.Longitude = "116.078631";
            request.Latitude = "39.71274";
            request.SubAppid = "wxid_129752pfasf1671";
            request.GoodsTag = "sz001";
            
            // 设置商品详情和终端信息
            request.GoodsData = BuildGoodsData();
            request.TerminalInfo = BuildTerminalInfo();
            
            return request;
        }

        /// <summary>
        /// 二维码支付示例
        /// </summary>
        public static async Task QrcodePayDemoAsync()
        {
            Console.WriteLine("开始执行二维码支付示例");
            
            // 使用DemoConfig创建配置对象
            var config = DemoConfig.CreateConfigWithCrypto();
            Console.WriteLine("配置初始化完成，已启用自动加密和解密功能");

            try
            {
                var qrcodePayService = new QrcodePayService(config);
                
                // 创建二维码支付请求
                var request = BuildQrcodePayRequest();
                
                // 发送请求
                var response = await qrcodePayService.QrcodePayAsync(request);
                
                // 使用优化的响应处理
                response.HandleResponse("二维码支付", config);
            }
            catch (Exception ex)
            {
                // 使用优化的异常处理
                var response = new QrcodePayResponse();
                response.HandleResponseWithException("二维码支付", config, ex);
            }
            
            Console.WriteLine("二维码支付示例执行完毕");
        }

        /// <summary>
        /// 主程序入口
        /// </summary>
        public static async Task Main(string[] args)
        {
            Console.WriteLine("=" + new string('=', 50));
            Console.WriteLine("===== 二维码支付示例 开始 =====");
            Console.WriteLine("=" + new string('=', 50));

            await QrcodePayDemoAsync();

            Console.WriteLine("=" + new string('=', 50));
            Console.WriteLine("===== 二维码支付示例执行完毕 =====");
            Console.WriteLine("=" + new string('=', 50));
        }
    }
} 